//go:build ignore

#include "common.h"

#include "bpf_tracing.h"

char __license[] SEC("license") = "Dual MIT/GPL";
#define MAX_DATA_LEN 256
struct event {
    u64 pid;
	u64 timestamp;
	u8 line[MAX_DATA_LEN];
    
};

struct bpf_map_def SEC("maps") kprobe_map = {
	.type        = BPF_MAP_TYPE_ARRAY,
	.key_size    = sizeof(u64),
	.value_size  = sizeof(struct event),
	.max_entries = 4096,
};

struct {
	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
} events SEC(".maps");


// Force emitting struct event into the ELF.
const struct event *unused __attribute__((unused));

SEC("uretprobe/exec_simple_query")
int uretprobe_exec_simple_query(struct pt_regs *ctx) {

	u64 pid = bpf_get_current_pid_tgid()>>32;
    struct event * valp = bpf_map_lookup_elem(&kprobe_map, &pid);
	if (valp) {

		valp->timestamp = bpf_ktime_get_ns()- (valp->timestamp);
        bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, valp, sizeof(struct event));
        bpf_map_delete_elem(&kprobe_map, &pid);
		return 0;
	}

	return 0;
}

SEC("uprobe/exec_simple_query")
int uprobe_exec_simple_query(struct pt_regs *ctx) {
	struct event event;

	u64 pid = bpf_get_current_pid_tgid()>>32;
    char *sql_string= (char *)PT_REGS_PARM1(ctx);
    event.pid = pid;
    bpf_probe_read(&event.line, sizeof(event.line), sql_string);
    bpf_map_update_elem(&kprobe_map,&pid,&event,BPF_ANY);

	return 0;
}

